package cn.itrip.search.service;

import cn.itrip.beans.vo.hotel.SearchHotCityVO;
import cn.itrip.beans.vo.hotel.SearchHotelVO;
import cn.itrip.common.EmptyUtils;
import cn.itrip.common.Page;
import cn.itrip.search.beans.vo.hotel.ItripHotelVO;
import cn.itrip.search.dao.BaseQuery;
import org.apache.solr.client.solrj.SolrQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("hotelSearchService")
public class HotelSearchServiceImpl implements HotelSearchService {
    @Resource
    private BaseQuery<ItripHotelVO> baseQuery;

    @Override
    public Page<ItripHotelVO> getHotelForPage(SearchHotelVO searchHotelVO) throws Exception {
        SolrQuery solrQuery = new SolrQuery("*:*");
        //构建全文检索q条件
        StringBuilder builder = new StringBuilder("");
        builder.append("destination:" + searchHotelVO.getDestination());
        if(EmptyUtils.isNotEmpty(searchHotelVO.getKeywords())){
            //xxx xxx
            searchHotelVO.setKeywords(searchHotelVO.getKeywords().replace(' ', '|'));
            builder.append(" AND keyword:" + searchHotelVO.getKeywords());
        }
        //构建过滤查询fq条件
        //商圈
        if(EmptyUtils.isNotEmpty(searchHotelVO.getTradeAreaIds())){
            //(a OR b OR c) AND
            //123,334,234
            //(tradingAreaIds:*,123,* OR tradingAreaIds:*,334,* OR tradingAreaIds:*,234,*)
            StringBuilder builder1 = new StringBuilder("(");
            String[] ids = searchHotelVO.getTradeAreaIds().split(",");
            boolean isFirst = true;
            for(String id:ids){
                if(isFirst){
                    builder1.append(" tradingAreaIds:*," + id + ",*");
                    isFirst = false;
                }else{
                    builder1.append(" OR tradingAreaIds:*," + id +",*");
                }
            }
            builder1.append(")");
            //设置fq查询条件
            solrQuery.addFilterQuery(builder1.toString());
        }
        //价格区间
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMaxPrice())){
            solrQuery.addFilterQuery(" minPrice:[* TO "+ searchHotelVO.getMaxPrice() +"]");
        }
        if(EmptyUtils.isNotEmpty(searchHotelVO.getMinPrice())){
            solrQuery.addFilterQuery(" maxPrice:[" + searchHotelVO.getMinPrice() + " TO *]");
        }
        //星级
        if(EmptyUtils.isNotEmpty(searchHotelVO.getHotelLevel())){
            solrQuery.addFilterQuery("hotelLevel:" + searchHotelVO.getHotelLevel());
        }
        //酒店特色
        if(EmptyUtils.isNotEmpty(searchHotelVO.getFeatureIds())){
            //(a OR b OR c) AND
            //123,334,234
            //(featureIds:*,123,* OR featureIds:*,334,* OR featureIds:*,234,*)
            StringBuilder builder1 = new StringBuilder("(");
            String[] ids = searchHotelVO.getFeatureIds().split(",");
            boolean isFirst = true;
            for(String id:ids){
                if(isFirst){
                    builder1.append(" featureIds:*," + id + ",*");
                    isFirst = false;
                }else{
                    builder1.append(" OR featureIds:*," + id +",*");
                }
            }
            builder1.append(")");
            //设置fq查询条件
            solrQuery.addFilterQuery(builder1.toString());
        }

        //设置q查询条件
        solrQuery.setQuery(builder.toString());
        //按
        if(EmptyUtils.isNotEmpty(searchHotelVO.getAscSort())){
            solrQuery.addSort(searchHotelVO.getAscSort(), SolrQuery.ORDER.asc);
        }
        if(EmptyUtils.isNotEmpty(searchHotelVO.getDescSort())){
            solrQuery.addSort(searchHotelVO.getDescSort(), SolrQuery.ORDER.desc);
        }
        return baseQuery.getHotelByPage(solrQuery, searchHotelVO.getPageNo(),
                searchHotelVO.getPageSize(), ItripHotelVO.class);
    }

    @Override
    public List<ItripHotelVO> getTopHotelByCityId(SearchHotCityVO searchHotCityVO) throws Exception {
        SolrQuery solrQuery = new SolrQuery("*:*");
        solrQuery.addFilterQuery("cityId:" + searchHotCityVO.getCityId());
        return baseQuery.getTop(solrQuery, searchHotCityVO.getCount(), ItripHotelVO.class);
    }
}
